home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / NString 1.0 beta / Sources / Alphabet.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  9.7 KB  |  242 lines  |  [TEXT/KAHL]

  1. /*    
  2.  
  3. _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  4.  
  5.         ALPHABET - Simplistic representation of a set of characters,
  6.             for use with the NString class. Could be replaced by a more general
  7.             alphabet representation, being a subclass of a general set-class.
  8.  
  9.         Version 1.0 - beta
  10.         
  11.         Copyright © 12th November, 1994 by Joël "Obiwan" François
  12.  
  13. _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  14.  
  15. */
  16.  
  17. #ifndef _ALPHABET_H_
  18. #define _ALPHABET_H_
  19.  
  20. #define BUFSIZE 32        /* 32 bytes == 256 characters / 8 bit */
  21. #define ELT_HIGH 255    /* the number of the highest possible element */
  22. #define ELT_LOW 0        /* the number of the lowest possible element */
  23.  
  24. class NString;
  25.  
  26. class Alphabet
  27. {
  28.     private:
  29.     
  30.         unsigned char buffer[BUFSIZE];                                // 256 bits to hold the presence state of each letter
  31.  
  32.     public:
  33.         
  34.         //    Note: All functions returning an Alphabet&, like "+=" etc., return a reference to *this
  35.         //            to allow function call chaining, e.g.:    b = (a += 'J') - c;
  36.         //            The example line first adds the letter 'J' to the alphabet a, then stores the difference
  37.         //            between the modified alphabet a and the alphabet c in the variable b.
  38.         
  39.         /*-------- Constructors & Destructor --------*/
  40.         
  41.         inline Alphabet (void);                                                                // Alphabet a;
  42.             //    Create an empty alphabet.
  43.             
  44.         Alphabet (const char *source);                                                        // Alphabet a("YNQ");
  45.             // Create an alphabet from a string: only the string's letters
  46.             //        will be members of the alphabet.
  47.  
  48.         Alphabet (const char source);                                                        // Alphabet a('D');
  49.             // Create an alphabet from a letter
  50.             
  51.         Alphabet (const Alphabet& source);                                                // Alphabet a(b);
  52.             // Create an alphabet from another alphabet.
  53.  
  54.         Alphabet (const NString& source);                                                // Alphabet a(s);
  55.             // Create an alphabet from an NString.
  56.  
  57.         Alphabet (const char start, const char end);                                    // Alphabet a('A', 'Z');
  58.             // Create an alphabet that contains all the letters
  59.             //        in the range from "start" to "end". If the given
  60.             //        range is empty (i.e. the starting letter's ASCII value
  61.             //        is greater than the ending letter's value), the constructed
  62.             //        alphabet will be empty. If start==end, the alphabet will
  63.             //        contain nothing but the specified letter.
  64.         
  65.         ~Alphabet (void) {}    
  66.  
  67.  
  68.         /*-------- General operations that affect the whole alphabet --------*/
  69.  
  70.         Alphabet& clear (void);                                                                // a.clear();
  71.             // Clear the alphabet.
  72.  
  73.  
  74.         /*-------- Assignment --------*/
  75.  
  76.         Alphabet& operator= (const char *source);                                    // a = "Obiwan";
  77.         Alphabet& operator= (const NString& source);                                // a = s;
  78.             // Reinitialize the alphabet with the letters of the given string.
  79.  
  80.         Alphabet& operator= (const char source);                                        // a = 'C';
  81.             // Reinitialize the alphabet with a single letter.
  82.  
  83.         Alphabet& operator= (const Alphabet& source);                                // a = b;
  84.             // Copy the source alphabet into this alphabet.
  85.  
  86.  
  87.         /*-------- Adding/Removing letters to/from the alphabet --------*/
  88.         
  89.         Alphabet& operator+= (const char element);                                    // a += 'q';
  90.             // Add the given element to the alphabet
  91.         
  92.         inline Alphabet operator+ (const char element) const;                        // a = b + 'q';
  93.             // Return a new alphabet which is constructed by
  94.             //        adding the given element to this alphabet
  95.  
  96.         Alphabet& operator-= (const char element);                                    // a -= 'q';
  97.             // Remove the given element from the alphabet
  98.         
  99.         inline Alphabet operator- (const char element) const;                        // a = b - 'q';
  100.             // Return a new alphabet which is constructed by
  101.             //        removing the given element from this alphabet
  102.  
  103.  
  104.         Alphabet& operator+= (const char *source);                                    // a += "Addendum";
  105.         Alphabet& operator+= (const NString& source);                            // a += s;
  106.             // Add the given string's letters to the alphabet
  107.         
  108.         inline Alphabet operator+ (const char *source) const;                    // a = b + "Addendum";
  109.         inline Alphabet operator+ (const NString& source) const;                // a = b + s;
  110.             // Return a new alphabet which is constructed by
  111.             //        adding the given string's letters to this alphabet
  112.  
  113.         Alphabet& operator-= (const char *source);                                    // a -= "2 B Removed";
  114.         Alphabet& operator-= (const NString& source);                                // a -= s;
  115.             // Remove the given string's letters from the alphabet
  116.         
  117.         inline Alphabet operator- (const char *source) const;                        // a = b - "2 B Removed";
  118.             // Return a new alphabet which is constructed by
  119.             //        removing the given string's letters from this alphabet.
  120.             // Note: An NString equivalent does not exist for this method
  121.             //        to avoid an ambiguity with the "operator-" function
  122.             //        declared in the NString class, which means: remove
  123.             //        initial occurrences of an alphabet from an NString.
  124.  
  125.  
  126.         /*-------- Testing the membership of one or more letters --------*/
  127.         
  128.         int contains (const char element) const;                                            // if (a.contains('f')) ...
  129.             //    Return 1 iff the given element is a member of the alphabet,
  130.             //        0 otherwise.
  131.  
  132.         int contains (const char *elements) const;                                        // if (a.contains("Hello")) ...
  133.         int contains (const NString& elements) const;                                // if (a.contains(s)) ...
  134.             // Return 1 iff the alphabet contains ALL OF the given string's letters
  135.  
  136.         /*-------- Logical operations that affect the whole alphabet --------*/
  137.  
  138.         Alphabet& operator+= (const Alphabet& other);                                // a += b;
  139.             //    Perform a logical OR between this alphabet and the other one,
  140.             //        then store the result in this alphabet.
  141.  
  142.         Alphabet& operator*= (const Alphabet& other);                                // a *= b;
  143.             //    Perform a logical AND between this alphabet and the other one,
  144.             //        then store the result in this alphabet.
  145.  
  146.         Alphabet& operator-= (const Alphabet& other);                                // a -= b;
  147.             //    Store the logical DIFFERENCE between this alphabet and the
  148.             //        other one in this alphabet.
  149.  
  150.         inline Alphabet operator+ (const Alphabet& other) const;                // a = b + c;
  151.             //    Perform a logical OR between this alphabet and the other one,
  152.             //        then return the result.
  153.  
  154.         inline Alphabet operator* (const Alphabet& other) const;                // a = b * c;
  155.             //    Perform a logical AND between this alphabet and the other one,
  156.             //        then return the result.
  157.  
  158.         inline Alphabet operator- (const Alphabet& other) const;                // a = b - c;
  159.             //    Return the logical DIFFERENCE between this alphabet and the other one.
  160.  
  161.  
  162.         inline Alphabet& operator*= (const char *string);                            // a *= "Hello";
  163.         inline Alphabet& operator*= (const NString& string);                    // a *= s;
  164.             //    Perform a logical AND between this alphabet and the one
  165.             //        defined by the letters of the string, then store the result in this alphabet.
  166.  
  167.         inline Alphabet operator* (const char *string) const;                        // a = b * "Hello";
  168.         inline Alphabet operator* (const NString& string) const;                // a = b * s;
  169.             //    Perform a logical AND between this alphabet and the one
  170.             //        defined by the letters of the string, then return the result.
  171.             
  172.             
  173.         Alphabet& complement (void);                                                        // a.complement();
  174.             // Transform this alphabet into its complement.
  175.             
  176.         inline Alphabet operator- (void) const;                                            // b = -a;
  177.             // Return this alphabet's complement.
  178.  
  179.  
  180.         /*-------- Iterator --------*/
  181.  
  182.         enum direction_t {FORWARD, BACKWARD};
  183.  
  184.         Alphabet& through (int (*action)(const char, Alphabet&), direction_t dir = FORWARD);
  185.             //    Traverse the alphabet in the specified directon, calling at each step the given function.
  186.             // The "action" function receives as parameters the current character
  187.             //        and a reference to the alphabet being traversed.
  188.             //    If the alphabet is empty, the iterator does nothing.
  189.             //    If the alphabet is altered during iteration, these modifications will NOT affect the iteration
  190.             //        process, as the alphabet being traversed is actually a copy of the original alphabet.
  191.             //    The "action" may interrupt the iteration process by returning a non-zero value.
  192.             //        Zero must be returned for the iteration to proceed until the end of the alphabet is reached.
  193.             //    The iterator returns a reference to the alphabet being traversed.
  194.             
  195.             //    Example: "a.through(&print, Alphabet::FORWARD)" where print() has been previously defined;
  196.  
  197.  
  198.         /*-------- Getting the number of alphabet elements --------*/
  199.  
  200.         unsigned int card (void) const;                                                        // n = a.card();
  201.             //    Return the number of elements in this alphabet.
  202.  
  203.  
  204.         /*-------- Comparisons --------*/
  205.  
  206.         // The function variants that take a string or an NString argument
  207.         //     construct a temporary alphabet from the string's letters to use as
  208.         // "other alphabet".
  209.  
  210.         int operator== (const Alphabet& other) const;                                // if (a == b) ...
  211.         int operator== (const char *other) const;                                        // if (a == "*-+/") ...
  212.         int operator== (const NString& other) const;                                    // if (a == s) ...
  213.             // Return 1 if this alphabet is equal to the other one, 0 otherwise.
  214.             
  215.         inline int operator!= (const Alphabet& other) const;                        // if (a != b) ...
  216.         inline int operator!= (const char *other) const;                                // if (a != "ABC") ...
  217.         inline int operator!= (const NString& other) const;                        // if (a != s) ...
  218.             // Return 1 if this alphabet is not equal to the other one, 0 otherwise.
  219.  
  220.         inline int operator<= (const Alphabet& other) const;                        // if (a <= b) ...
  221.         inline int operator<= (const char *other) const;                                // if (a <= "0123456789") ...
  222.         inline int operator<= (const NString& other) const;                        // if (a <= s) ...
  223.             // Return 1 if this alphabet is contained in
  224.             //        or equal to the other one, 0 otherwise.
  225.  
  226.         inline int operator< (const Alphabet& other) const;                            // if (a < b) ...
  227.         inline int operator< (const char *other) const;                                // if (a < "1234") ...
  228.         inline int operator< (const NString& other) const;                            // if (a < s) ...
  229.             // Return 1 if this alphabet is contained in,
  230.             //        but not equal to the other one, 0 otherwise.
  231. };
  232.  
  233. #include "Alphabet_Inlines.h"
  234.  
  235. #ifndef KEEP_ALPHABET_DEFINES
  236.     #undef BUFSIZE
  237.     #undef ELT_HIGH
  238.     #undef ELT_LOW
  239. #endif
  240.  
  241. #endif
  242.